<template>
  <div class="container page-promote-view process4">
    <a id="download-model" :href="download.url"></a>
    <div class="tips">
      <div class="tips-icon"><img :src="getRequireImg('xiao-xi@2x')" alt=""></div>
      <div>请填写您的真实信息，确保获取准确的额度</div>
    </div>
    <div class="form">
      <div class="title-label">基本信息</div>
      <ul>
        <li>
          <div class="li-label">真实姓名</div>
          <input class="put" type="text" v-model="postData.name" placeholder="请输入您的真实姓名">
        </li>
        <li>
          <div class="li-label">身份证</div>
          <input class="put" type="text" v-model="postData.idcard" placeholder="请输入您的真实身份证号">
        </li>
        <li>
          <x-address title="常住地" v-model="cityValue" :list="onlineCity" :hide-district="true" @on-hide="setCity"></x-address>
          <div class="picker-placeholder" v-if="city.length < 1">
            <span>请选择</span>
            <img :src="getRequireImg('arror_bottom@2x')" alt="">
          </div>
        </li>
        <li>
          <div class="li-label">信用情况</div>
          <div class="show-popup" @click="showPopup('xinyong')">
            <template v-if="postData.credit">
              <span class="active">{{credit}}</span>
            </template>
            <template v-else>
              <span>请选择</span>
              <img :src="getRequireImg('arror_bottom@2x')" alt="">
            </template>
          </div>
          <div v-transfer-dom>
            <popup v-model="xinyongPopup" position="bottom" max-height="50%">
              <div class="popup-box">
                <div :class="['popup-li',{'active': item.id == postData.credit}]" v-for="(item,key) in xinyong" @click="change('xinyong', item)" :key="`xinyong-${key}`">
                  {{item.title}}
                  <img v-if="item.id == postData.credit" :src="getRequireImg('xuan-zhong@2x')" />
                </div>
              </div>
            </popup>
          </div>
        </li>
        <li>
          <div class="li-label">芝麻分</div>
          <div class="show-popup" @click="showPopup('zhimafen')">
            <template v-if="postData.sesame">
              <span class="active">{{sesame}}</span>
            </template>
            <template v-else>
              <span>请选择</span>
              <img :src="getRequireImg('arror_bottom@2x')" alt="">
            </template>
          </div>
          <div v-transfer-dom>
            <popup v-model="zhimafenPopup" position="bottom" max-height="50%">
              <div class="popup-box">
                <div :class="['popup-li',{'active': item.id == postData.sesame}]" v-for="(item,key) in zhimafen" @click="change('zhimafen', item)" :key="`zhimafen-${key}`">
                  {{item.title}}
                  <img v-if="item.id == postData.sesame" :src="getRequireImg('xuan-zhong@2x')" />
                </div>
              </div>
            </popup>
          </div>
        </li>
      </ul>
    </div>
    <div class="submit" @click="onlineSubmit">
      <img :src="getRequireImg('btn@2x')" alt="">
      <div>确定</div>
    </div>
    <!-- 弹窗 -->
    <wechat-mask v-if="WeChat_show"></wechat-mask>

    
    <template>
      <x-dialog
        v-model="alertDialog"
        class="alert-dialog-box"
        :dialog-style="{
          'max-width': '100%',
          width: '87%',
          'background-color': '#fff',
          overflow: 'hidden',
          'border-radius': '8px',
        }"
      >
        <div style="text-align: center; font-size: 0.875rem;padding: 30px 11px;">
          <div class="close-btn" @click="closeDialog"><img :src="getRequireImg('cha@2x')" alt=""></div>
          <div class="dialog-title">恭喜您已通过预审！</div>
          <div class="dialog-text">您的预估额度为：</div>
          <div class="price-box">
            <div>60000</div>
            <span>元</span>
          </div>
          <div class="toProcess" @click="toProcess">立即提现<span>{{alert.text}}</span></div>
        </div>
      </x-dialog>
    </template>
    
  </div>
</template>

<style lang="scss" scoped>
.page-promote-view.process4{
  padding: 12px;
  min-height: 100vh;
  background-color: #F5F5F5;
  .tips{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    background-color: #FEF4DF;
    border-radius: 20px;
    margin-bottom: 12px;
    height: 26px;
    padding-left: 12px;
    font-size: 12px;
    color: #F1B941;
    .tips-icon{
      width: 14px;
      margin-right: 6px;
      img{
        display: block;
        max-width: 100%;
      }
    }
  }
  
  .form{
    background-color: #fff;
    box-shadow: 1px 1px 6px rgba(0, 0, 0, 1);
    border-radius: 8px;
    padding: 10px 0 14px;
    margin-bottom: 60px;
    .title-label{
      font-weight: bold;
      font-size: 16px;
      color: #333;
      padding: 0 12px;
    }
    li{
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 53px;
      padding: 0 12px;
      border-bottom: 1px solid #E4E4E4;
      position: relative;
      .vux-cell-box{
        width: 100%;
        z-index: 1;
        /deep/ .weui-cell{
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 0;
          .weui-cell__hd{
            font-size: 14px;
          }
        }
      }
      .li-label{
        font-size: 14px;
      }

      .picker-placeholder,
      .show-popup{
        display: flex;
        &.picker-placeholder{
          position: absolute;
          right: 12px;
        }
        span,
        img{
          display: block;
        }
        span{
          font-size: 12px;
          color: #969799;
          &.active{
            color: #383838;
          }
        }
        img{
          width: 10px;
          height: 10px;
          margin-left: 10px;
          display: block;
        }
      }

      .put{
        border: 0;
        text-align: right;
        flex:1;
        &::placeholder{
          color: #AEAEAE;
          font-size: 12px;
        }
      }
    }
  }
  .submit{
    width: 350px;
    margin: auto;
    position: relative;
    img{
      display: block;
      max-width: 100%;
    }
    >div{
      color: #fff;
      font-size: 18px;
      padding-bottom: 8px;
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.vux-popup-dialog{
  border-radius: 8px 8px 0 0;
}
.popup-box{
  .popup-li{
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #E4E4E4;
    font-size: 16px;
    color: #383838;
    position: relative;
    &.active{
      color: #3478F2;
    }
    &:last-child{
      border-bottom: 0;
    }
    img{
      width: 15px;
      display: block;
      position: absolute;
      right: 24px;
    }
  }
}

.alert-dialog-box{
  position: relative;
  .close-btn{
    position: absolute;
    right: 6px;
    top: 14px;
    z-index: 99;
    width: 22px;
    padding: 3px 4px;
    img{
      max-width: 100%;
      display: block;
    }
  }
  .dialog-title{
    font-weight: bold;
    font-size: 22px;
    color: #333;
    margin-bottom: 6px;
  }
  .dialog-text{
    font-size: 16px;
    color: #333333;
  }
  .price-box{
    color: #FC1F1F;
    font-size: 40px;
    height: 56px;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    margin: 10px 0;
    >div{
      height: 50px;
    }
    span{
      height: 32px;
      font-size: 18px;
    }
  }
  .toProcess{
    background-color: #3478F2;
    color: #fff;
    font-weight: bold;
    text-align: center;
    line-height: 48px;
    width: 304px;
    height: 48px;
    border-radius: 8px;
    font-size: 22px;
    margin: 0 auto 20px;
    span{
      font-size: 18px;
      font-weight: normal;
    }
  }
}
</style>

<script>
  /*global $ returnCitySN require ClipboardJS wx err process*/
import { getImg, isIDCard, isUsername } from "@/service/util.helper";
import { Popup, TransferDom, XAddress, ChinaAddressV4Data } from "vux";
import WechatMask from "@/components/WechatMask";
import iosDownload from "@/mixins/iosDownload";

export default {
  directives: {
    TransferDom
  },
  components: {
    Popup,
    XAddress,
    WechatMask,
  },
  props: {},
  data() {
    return {
      data: {},
      
      download: {
        url: "",
        show: false,
      },
      alertDialog:false,
      WeChat_show: false,
      guide: this.$route.query.id || 0,
      
      onlineData: {},
      cityValue: [],
      defaultCity: [],
      onlineCity: ChinaAddressV4Data,
      
      show13:false,
      
      format: function (value, name) {
        return `${value[0]}:${value[1]}`
      },
      xinyongPopup:false,
      xinyong:[{
        id:"1",
        title:'信用良好，无逾期',
      },{
        id:"2",
        title:'无信用卡或贷款',
      },{
        id:"3",
        title:'近1年无逾期',
      },{
        id:"4",
        title:'1年内逾期少于3次且少于90',
      },{
        id:"5",
        title:'1年内逾期少于3次或者90天',
      }],
      zhimafenPopup:false,
      zhimafen:[{
        id:"1",
        title:'550分以下',
      },{
        id:"2",
        title:'550-579分',
      },{
        id:"3",
        title:'580-619分',
      },{
        id:"4",
        title:'620-649分',
      },{
        id:"5",
        title:'699-650分',
      },{
        id:"6",
        title:'700分以上',
      }],
      alert: {
        text:'（3s）',
        countTime: 3,
        count: 0,
      },

      sesame:'',
      credit:'',
      postData:{
        sesame:'',
        credit:'',
        station: "",
        name:'',
        idcard:'',
      }
    };
  },
  mixins:[iosDownload],
  watch:{
    
  },
  computed: {
    city(){
      if(this.cityValue.length > 0){
        return this.cityValue
      }else{
        return this.defaultCity
      }
    }
  },
  methods: {
    getRequireImg(name, is64 = 0) {
      let imgObj = {
        name: name,
        is64: is64,
        download_page: null,
        flooring_page: 167,
      };
      return getImg(imgObj);
    },
     
    goDownload() {
      $("#download-model")[0].click();
      this.iosDownloadPage()
    },
    iosWechatBlank() {
      // Wechat leave blank compatible
      let ua = this.$router.UA();
      if (ua.ios && ua.weixin) {
        window.scroll(0, 0);
      }
    },
    getOnlineCityData(pid) {
      if(pid){
        this.defaultCity.unshift(pid)
      }
      this.onlineCity.map(v=>{
        if(pid){
          if(v.value == pid && v.parent){
            this.getOnlineCityData(v.parent)
          }
        }else{
          if(v.value == returnCitySN.cid && v.parent){
            this.defaultCity.unshift(v.value)
            this.getOnlineCityData(v.parent)
          }
        }
      })
      this.setCity()
    },
    checkData(){
      let ruleName = "";
      if(!isIDCard(this.postData.idcard)){
        ruleName = "身份证"
        this.$vux.toast.text('请输入正确身份证',"middle");
      }
      if(!isUsername(this.postData.name)){
        ruleName = "姓名";
        this.$vux.toast.text('请输入正确姓名',"middle");
      }
      if(this.postData.address){
        ruleName = "地址";
        this.$vux.toast.text('请选择正确地址',"middle");
      }
      if(!this.postData.sesame){
        ruleName = "芝麻分";
        this.$vux.toast.text('请选择芝麻分',"middle");
      }
      if(!this.postData.credit){
        ruleName = "信用情况";
        this.$vux.toast.text('请选择信用情况',"middle");
      }

      return ruleName
    },
    onlineSubmit() {
      let ruleObj = this.checkData();
      if (ruleObj !== "") return;

      // this.toProcess()
      this.$vux.loading.show();

      this.$http
        .post(`/landing/idcardcheck`, {
          ...this.$route.query,
          ...this.postData
        })
        .then(({ data }) => {
          this.$vux.loading.hide();
          if (data.code === 200) {

            this.alertDialog = true;
            this.alert.count = this.alert.countTime;
            let countdown = setInterval(() => {
              this.alert.count--;
              this.alert.text = `（${this.alert.count}s）`;
              if (this.alert.count === 0) {
                this.alert.text = "";
                clearInterval(countdown);
              }
            }, 1000);
          } else {
            this.$vux.toast.text(data.msg, "middle");
          }
        })
        .catch((error) => {
          this.$vux.loading.hide();
          this.$vux.toast.text("服务器繁忙！", "middle");
        });
    },
    setCity(){
      var cityArr = []
      this.onlineCity.map(v=>{
        this.city.map((vv,k)=>{
          if(v.value == vv){
            cityArr.push(v.name)
          }
        })
      })
      this.postData.station = cityArr.join('-')
    },
    toProcess(){
      this.$router.push({
        path:'/process/view2',
      })
    },
    showPopup(type){
      if(type == 'xinyong'){
        this.xinyongPopup = true;
      }
      if(type == 'zhimafen'){
        this.zhimafenPopup = true;
      }
    },
    change(type,item){
      if(type == 'xinyong'){
        this.postData.credit = item.id;
        this.xinyongPopup = false;
        this.credit = item.title;
      }
      if(type == 'zhimafen'){
        this.postData.sesame = item.id;
        this.zhimafenPopup = false;
        this.sesame = item.title;
      }
    },
    closeDialog(){
      this.alertDialog = false;
    },
  },
  created() {
    let ua = this.$router.UA();
    this.download.url = sessionStorage.getItem('download_url');
    this.getOnlineCityData();
  },
  mounted(){
    if (this.$router.UA().weixin) {
      this.WeChat_show = true;
      return false;
    }
  }
};
</script>
